//
// Created by wsgalaxy on 2/23/18.
//
/*
#include "gndapp.h"
#include "gndmainwin.h"

#include "gndinitmodule.h"
#include "gndmodule.h"
*/

#include "gnd.h"

struct _GndApp {
    GtkApplication parent;
    GndMainwin * mainwin;
    GndConnect * connect;

    GndModule * init_module;
    GndModule * device_info_module;
    GndModule * screencap_module;
};

G_DEFINE_TYPE(GndApp, gnd_app, GTK_TYPE_APPLICATION)

typedef enum _GndAppSignal {
    GND_APP_SIGNAL_CONNECT,
    GND_APP_SIGNAL_DISCONNECT,
    GND_APP_SIGNAL_LAST_SIGNAL
} GndAppSignal;

int gnd_app_signal[GND_APP_SIGNAL_LAST_SIGNAL];

void gnd_app_activate(GndApp * app, gpointer data);
void gnd_app_init_finish(GndApp * app, GndInitModuleResult result, GndInitModule * module);
void gnd_app_emit_connect(GndApp * self, GndInitModule * module);
void gnd_app_emit_disconnect(GndApp * self, GndInitModule * module);

void gnd_app_init(GndApp *self)
{
    //self->init_module = GND_MODULE(gnd_init_module_new(self));
}

void gnd_app_class_init(GndAppClass *klass)
{
    gnd_app_signal[GND_APP_SIGNAL_CONNECT] = g_signal_new("connect",
                                                       GND_TYPE_APP,
                                                       G_SIGNAL_RUN_FIRST,
                                                       0,
                                                       NULL,
                                                       NULL,
                                                       g_cclosure_marshal_VOID__VOID,
                                                       G_TYPE_NONE,
                                                       0);
    gnd_app_signal[GND_APP_SIGNAL_DISCONNECT] = g_signal_new("disconnect",
                                                       GND_TYPE_APP,
                                                       G_SIGNAL_RUN_FIRST,
                                                       0,
                                                       NULL,
                                                       NULL,
                                                       g_cclosure_marshal_VOID__VOID,
                                                       G_TYPE_NONE,
                                                       0);
}


GndApp * gnd_app_new(const char *app_id, GApplicationFlags flags)
{
    GndApp * app = (GndApp *) g_object_new(GND_TYPE_APP,
                                           "application_id", app_id,
                                           "flags", flags,
                                           NULL);
    g_signal_connect(app, "activate", G_CALLBACK(gnd_app_activate), NULL);

    return app;
}

//app启动
void gnd_app_activate(GndApp *app, gpointer data)
{
    //GtkApplicationWindow * win = gtk_application_window_new(app);
    app->mainwin = gnd_mainwin_new();
    app->connect = gnd_connect_new(app);
    app->init_module = GND_MODULE(gnd_init_module_new(app));
    app->device_info_module = GND_MODULE(gnd_device_info_module_new(app));
    app->screencap_module = GND_MODULE(gnd_screencap_module_new(app));

    gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW(app->mainwin));
    gtk_widget_show_all(GTK_WIDGET(app->mainwin));

    //gnd_connect_init(app->connect);

    g_signal_connect_swapped(app->init_module, "init-finish", G_CALLBACK(gnd_app_init_finish), app);

    g_signal_connect_swapped(app->init_module, "connect", G_CALLBACK(gnd_app_emit_connect), app);
    g_signal_connect_swapped(app->init_module, "disconnect", G_CALLBACK(gnd_app_emit_disconnect), app);

    //GndInitModule * init_module = gnd_init_module_new(app);
    gnd_module_start(app->init_module, (gpointer) app);
    gnd_module_start(app->device_info_module, (gpointer) app);
    gnd_module_start(app->screencap_module, (gpointer) app);

}


char * gnd_app_get_setting_str(GndApp * self,  char * name)
{
    if (!g_strcmp0(name, "adb_path")) {
        return "/bin/adb";
    }

    return NULL;
}

gboolean gnd_app_set_setting_str(GndApp *self, char *name, char *data)
{
    return TRUE;
}


void gnd_app_init_finish(GndApp *app, GndInitModuleResult result, GndInitModule *module)
{
    g_print("init finished\n");
}


GndMainwin * gnd_app_get_mainwin(GndApp * self)
{
    g_assert(GND_IS_APP(self));
    return self->mainwin;
}


GndConnect * gnd_app_get_connect(GndApp * self)
{
    return self->connect;
}


void gnd_app_emit_connect(GndApp * self, GndInitModule * module)
{
    g_signal_emit_by_name(self, "connect", NULL);
    g_message("GndApp: emit connect");
}


void gnd_app_emit_disconnect(GndApp * self, GndInitModule * module)
{
    g_signal_emit_by_name(self, "disconnect", NULL);
    g_message("GndApp: emit disconnect");
}
